package com.learn.gateway.filter;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

/**
 * @author :wm
 * @description :局部过滤器
 */
@Component
public class MyPartGatewayFilterFactory extends AbstractGatewayFilterFactory<MyPartGatewayFilterFactory.Config> {
    private final static Logger logger = LoggerFactory.getLogger(MyPartGatewayFilterFactory.class);

    /**
     * 必须，否则会报Config不能转换为Object
     */
    public MyPartGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        logger.info(String.format("name:%s,value:%s", config.getName(), config.getValue()));
        return (exchange, chain) -> chain.filter(exchange);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(Config.FIELD_REAL_NAME, Config.FIELD_VALUE);
    }

    public static class Config {

        public final static String FIELD_REAL_NAME = "name";
        public final static String FIELD_VALUE = "value";

        public String name;
        public String value;

        public String getName() {
            return name;
        }

        public Config setName(String name) {
            this.name = name;
            return this;
        }

        public String getValue() {
            return value;
        }

        public Config setValue(String value) {
            this.value = value;
            return this;
        }
    }
}
